package me.pplay.playtalk.adapter;


import java.util.List;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.holder.HomeHolder;
import me.pplay.playtalk.protocol.bean.TPBuddyinfo;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HomeAdapter extends BaseAdapter{
	private Context mContext;
	private LayoutInflater inflater;
	private String[] titles;
	private int[] drawableIds =  new int[]{R.drawable.home_message_icon_selector, R.drawable.home_contacts_icon_selector,R.drawable.discovery,R.drawable.home_community_icon_selector,R.drawable.home_setting_icon_selector};

	public HomeAdapter(Context context){
		mContext		= context;
		inflater		= LayoutInflater.from(context);
		titles			= context.getResources().getStringArray(R.array.array_home);
	}

	@Override
	public int getCount() {
		return titles.length;
	}

	@Override
	public Object getItem(int arg0) {
		return titles[arg0];
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HomeHolder holder;
		if(convertView == null){
			holder				= new HomeHolder();
			convertView 		= (View)inflater.inflate(R.layout.home_list_item, null);
			holder.icon			= (ImageView)convertView.findViewById(R.id.icon);
			holder.title		= (TextView)convertView.findViewById(R.id.title);
			holder.signatureTv	= (TextView)convertView.findViewById(R.id.signatureTv);
			convertView.setTag(holder);
		}else{
			holder	= (HomeHolder)convertView.getTag();
		}
		holder.title.setText(titles[position]);
		holder.icon.setBackgroundResource(drawableIds[position]);
		
		if(position == 0){
//			holder.signatureTv.setText("4" + mContext.getString(R.string.home_sign_message));
			holder.signatureTv.setVisibility(View.GONE);
		}else if(position == 1){
			int count = 0;
			List<TPBuddyinfo> list = ((ApplicationTrans)mContext.getApplicationContext()).getTpChat().tp_chat_getfriendslist();
			if(list != null)
				count = list.size();
			holder.signatureTv.setText(count +" "+ mContext.getString(R.string.friends));
			holder.signatureTv.setVisibility(View.VISIBLE);
		}else if(position == 2){
			holder.signatureTv.setText(mContext.getString(R.string.home_discovery));
			holder.signatureTv.setVisibility(View.VISIBLE);
		}else{
			holder.signatureTv.setVisibility(View.GONE);
		}
		return convertView;
	}
}
